
import {
    createRouter,
    createWebHistory,
    createWebHashHistory,
    type RouteRecordRaw
  } from 'vue-router'
  
  import layout from '@/layout/layout.vue'
  
  const constantRoutes: Array<RouteRecordRaw> = [
    {
      path: '/',
      redirect: '/home'
    },
    {
      path: '/',
      name: 'layout',
      component: layout,
      children: [
        {
          path: "/home",
          name: "home",
          meta: {
            title: '首页介绍',
            openInNewWindow: true
          },
          component: () => import("@/views/home/index.vue"),
        },
        {
          path: "/:pathMatch(.*)*",
          redirect:'/404',
        },
        {
          path: "/404",
          name: "NotFound",
          meta: {
            title: '404',
            openInNewWindow: false
          },
          component: () => import("@/views/error/404.vue"),
        },
      ]
    },
    
  ]
  
  const router = createRouter({
    history: createWebHashHistory(import.meta.env.BASE_URL),
    routes: constantRoutes
  })
  
  // 路由守卫，鉴权
  router.beforeEach(async (to, from, next) => {
   
    (document as any).title = to.meta.title
    localStorage.setItem('activePath', to.path)
    if(to.name){
     next()
    }
  })
  
  router.afterEach(() => {
    
  })
  
  export default router